<?php
namespace app\admin\controller;
use think\Db;
use think\Session;
use think\Request;
use think\Controller;
use think\File;

class Area extends Controller
{
	/**
	 * 加载专区管理页面
	 * @return [type] [description]
	 */
	public function index()
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		$list = Db::table('ubisoft_area a,ubisoft_section s')->field('a.id,a.a_name,a.ctime,s.name')->where('a.sec_id = s.id')->select();
		// var_dump($list);
    	return $this->fetch('list',['title'=>'蒸汽男孩后台','nick'=>$name['name'],'list'=>$list]);
	}

	/**
	 * 加载专区添加页面
	 */
	public function add()
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		$list = Db::name('section')->select();
		return $this->fetch('add',['title'=>'蒸汽男孩后台','list'=>$list,'nick'=>$name['name']]);
	}

	/**
	 * 执行专区添加逻辑
	 * @param  Request $req [description]
	 * @return [type]       [description]
	 */
	public function insert(Request $req)
	{
		$sql = $req->post();
		$file['icon'] = request()->file('icon');
		$file['bg'] = request()->file('bg');
		if($file['icon'] == null || $file['bg'] == null) {
			$this->error('请先上传图片');
		} elseif($sql['a_name'] == '') {
			$this->error('请填写表单');
		}

		if($file['icon'] || $file['bg']){
	        $info['icon'] = $file['icon']->move(ROOT_PATH . 'public' . DS . 'uploads');
	        $info['bg'] = $file['bg']->move(ROOT_PATH . 'public' . DS . 'uploads');
	    }
	    $icon = $info['icon']->getSaveName();
	    $bg = $info['bg']->getSaveName();
		// var_dump($icon);die;
	    $sql['ctime'] = date('Y-m-d H:i:s',time());
		$data = Db::name('area')->insert(['a_name'=>$sql['a_name'],'sec_id'=>$sql['sec_id'],'ctime'=>$sql['ctime'],'icon'=>$icon,'bg'=>$bg]);

		if($data > 0) {
			$this->success('增加成功',url('admin/area/index'));
		} else {
			$this->error('增加失败');
		}
	}

	/**
	 * 加载专区编辑页面
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function edit($id)
	{
		$name = Db::name('admin')->field('name')->where('name = "'.Session::get('admin').'"')->find();

		$list = Db::name('section')->select();
		$data = Db::name('area')->find($id);

		// var_dump($data);
		return $this->fetch('edit',['title'=>'蒸汽男孩后台','data'=>$data,'list'=>$list,'id'=>$id,'nick'=>$name['name']]);
	}

	/**
	 * 执行专区更新逻辑
	 * @param  Request $req [description]
	 * @param  [type]  $id  [description]
	 * @return [type]       [description]
	 */
	public function update(Request $req,$id)
	{
		$sql = $req->post();

		$file['icon'] = request()->file('icon');
		$file['bg'] = request()->file('bg');

		if($file['icon'] == null && $file['bg'] == null) {
			$this->error('请先上传图片');
		} elseif($sql['a_name'] == '') {
			$this->error('请填写表单');
		}

		if($file['icon'] != null && $file['bg'] != null){
	    	$info['icon'] = $file['icon']->move(ROOT_PATH . 'public' . DS . 'uploads');
	    	$info['bg'] = $file['bg']->move(ROOT_PATH . 'public' . DS . 'uploads');
	    	$icon = $info['icon']->getSaveName();
	    	$bg = $info['bg']->getSaveName();
	    	$data = Db::name('area')->where(['id'=>$id])->update(['a_name'=>$sql['a_name'],'sec_id'=>$sql['sec_id'],'icon'=>$icon,'bg'=>$bg]);
	    } elseif($file['icon'] != null) {
	    	$info['icon'] = $file['icon']->move(ROOT_PATH . 'public' . DS . 'uploads');
	    	$icon = $info['icon']->getSaveName();
	    	$data = Db::name('area')->where(['id'=>$id])->update(['a_name'=>$sql['a_name'],'sec_id'=>$sql['sec_id'],'icon'=>$icon]);
	    } elseif($file['bg'] != null) {
	    	$info['bg'] = $file['bg']->move(ROOT_PATH . 'public' . DS . 'uploads');
	    	$bg = $info['bg']->getSaveName();
	    	$data = Db::name('area')->where(['id'=>$id])->update(['a_name'=>$sql['a_name'],'sec_id'=>$sql['sec_id'],'bg'=>$bg]);
	    } else {
	    	$data = Db::name('area')->where(['id'=>$id])->update(['a_name'=>$sql['a_name'],'sec_id'=>$sql['sec_id']]);
	    }

	    if($data > 0) {
			$this->success('编辑成功',url('admin/area/index'));
		} else {
			$this->error('编辑失败');
		}
	}

	/**
	 * 执行专区删除逻辑
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function delete($id)
	{
		$data = Db::name('area')->delete($id);

		if($data > 0) {
			$this->success('删除成功',url('admin/area/index'));
		} else {
			$this->error('删除失败');
		}
	}
}